from urllib.request import urlopen, Request
from http.client import HTTPResponse

import time

firefox_cookie = '_uuid=16F15B6C-3651-403E-0A38-2D793742832501207infoc; buvid3=' + \
                 '551F0C65-27D6-403E-BB6B-C9D9AF2DFBA953917infoc'


def my_urlopen(url: Request) -> HTTPResponse:
    err = Exception()
    for _ in range(3):
        try:
            resp = urlopen(url=url)  # type: HTTPResponse
        except Exception as e:
            err = e
            print(e)
        else:
            time.sleep(0.3)
            return resp
    with open(file='./errors.data', mode='a', encoding='utf-8') as f:
        f.write(url.get_full_url() + '\n')
        f.write(str(err) + '\n')
        f.write('\n')
    raise Exception('HTTP请求失败！')
